package JUC_stady;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Project: tobe-better-coder
 * Package: JUC_stady
 * 线程的中断机制
 *
 * @author : Mr.xu
 * @date : 2023/3/17 0:21
 */
public class InterruptDemo {

    volatile static boolean flag = false;

    static AtomicBoolean flag1 = new AtomicBoolean(false);

    public static void main(String[] args) {

//        method1();
//        method2();
//        method3();
        method4();
    }

    public static void method5() {

        Thread t1 = new Thread(() -> {
//            long start = System.currentTimeMillis();
            while (true) {
                //如果中断标识被打为true，则中止进程
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("线程" + Thread.currentThread().getName() + "中断标识是0" + Thread.currentThread().isInterrupted());
//                    System.out.println("线程"+Thread.currentThread().getName()+"收到通知，终止线程,耗时"+(System.currentTimeMillis()-start));
                    break;
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(20);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().isInterrupted() + "hello InterruptDemo");
            }
        });
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Thread t2 = new Thread(() -> {
            t1.interrupt();
            t1.interrupted();
        });
        t2.start();

    }

    //测试，当当前线程处于sleep状态的时候，另外的线程来设置中断状态，会报错
    public static void method4() {

        Thread t1 = new Thread(() -> {
//            long start = System.currentTimeMillis();
            while (true) {
                //如果中断标识被打为true，则中止进程
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("线程" + Thread.currentThread().getName() + "中断标识是0" + Thread.currentThread().isInterrupted());
//                    System.out.println("线程"+Thread.currentThread().getName()+"收到通知，终止线程,耗时"+(System.currentTimeMillis()-start));
                    break;
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(20);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().isInterrupted() + "hello InterruptDemo");
            }
        });
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Thread t2 = new Thread(() -> {
            t1.interrupt();
        });
        t2.start();

    }


    public static void method3() {

        Thread t1 = new Thread(() -> {
            long start = System.currentTimeMillis();
            while (true) {
                //如果中断标识被打为true，则中止进程
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("线程" + Thread.currentThread().getName() + "中断标识是0" + Thread.currentThread().isInterrupted());
                    System.out.println("线程" + Thread.currentThread().getName() + "收到通知，终止线程,耗时" + (System.currentTimeMillis() - start));
                    break;
                }
                System.out.println(Thread.currentThread().isInterrupted() + "hello InterruptDemo");
            }
        });
        t1.start();
        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Thread t2 = new Thread(() -> {
            t1.interrupt();
        });
        t2.start();

    }

    public static void method2() {
        new Thread(() -> {
            long start = System.currentTimeMillis();
            while (true) {
                if (flag1.get()) {
                    System.out.println("线程" + Thread.currentThread().getName() + "收到通知，终止线程,耗时" + (System.currentTimeMillis() - start));
                    break;
                }
                System.out.println("hello InterruptDemo");
            }
        }, "t1").start();

        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            flag1.set(true);
        }, "t2").start();
    }


    public static void method1() {
        new Thread(() -> {
            long start = System.currentTimeMillis();
            while (true) {
                if (flag) {
                    System.out.println("线程" + Thread.currentThread().getName() + "收到通知，终止线程,耗时" + (System.currentTimeMillis() - start));
                    break;
                }
                System.out.println("hello InterruptDemo");
            }
        }, "t1").start();

        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            flag = true;
        }, "t2").start();
    }
}